package com.haoshuang.zhouzhoubang.ui.page.order.contact

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityContactWorkerBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderWorkerBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.callPhone
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  
 */
class ContactWorkerActivity : BaseVMActivity<ContactWorkerViewModel, ActivityContactWorkerBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<ContactWorkerActivity>()
        }
    }

    private val orderId by lazy { intent.getString("order_id") }

    private val data = arrayListOf<OrderWorkerBean>()
    private val adapter by lazy { ContactWorkerAdapter(data) }

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "联系师傅"
            titleBar.leftImageView().click { finish() }

            recyclerView.vertical().adapter = adapter
            adapter.setOnItemClick { view, position, item ->
                if (item == null) return@setOnItemClick
                callPhone(item.phone?:"")
            }
        }
    }

    override fun initData() {
        vm.getOrderServiceMember(orderId)
    }

    override fun startObserve() {
        vm.getOrderServiceMemberData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?: mutableListOf())
                adapter.notifyDataSetChanged()
            }
        }
    }

}